﻿using Dapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.LedgerManagements;
using Management.Systeam.Read.Apis.Application.Command.Useres;
using Management.Systeam.Read.Apis.Auto.UserDto;
using MediatR;
using System.Text;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.LedgerManagements
{
    public class ShowLedgerManagementCommandHandler : IRequestHandler<ShowLedgerManagementCommand, ApiPeaging<ApiResult<List<LedgerManagement>>>>
    {

        IBaseRepository<LedgerManagement> LedgerManagement;
        public DatabaseContext db;

        public ShowLedgerManagementCommandHandler(IBaseRepository<LedgerManagement> LedgerManagement, DatabaseContext db)
        {
            this.LedgerManagement = LedgerManagement;
            this.db = db;
        }

        public async Task<ApiPeaging<ApiResult<List<LedgerManagement>>>> Handle(ShowLedgerManagementCommand request, CancellationToken cancellationToken)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM LedgerManagement");
            sb.Append(" WHERE IsDel LIKE '%" + 0 + "%'");
            if (request.Product_Code != null)
            {
                sb.Append(" AND Product_Code LIKE '%" + request.Product_Code + "%'");
            }
            if (request.Product_Type != null)
            {
                sb.Append(" AND Product_Type LIKE '%" + request.Product_Type + "%'");
            }
            if (request.NumberData != null)
            {
                sb.Append(" AND NumberData LIKE '%" + request.NumberData + "%'");
            }
            if (request.Weight != null)
            {
                sb.Append(" AND Weight LIKE '%" + request.Weight + "%'");
            }
            if (request.Warehouse_Name != null)
            {
                sb.Append(" AND Warehouse_Name LIKE '%" + request.Warehouse_Name + "%'");
            }
            if (request.Product_Status != null)
            {
                sb.Append(" AND Product_Status LIKE '%" + request.Product_Status + "%'");
            }
            var paramet = new
            {
                Product_Code = request.Product_Code,
                Product_Type = request.Product_Type,
                NumberData = request.NumberData,
                Weight = request.Weight,
                Product_Status = request.Product_Status,

                PageSize = request.PageSize,
                offset = (request.PageIndex - 1) * request.PageSize
            };
            var qtlist = await db.DapperConnection.QueryAsync<LedgerManagement>(sb.ToString(), paramet);
            int total = qtlist.Count();
            sb.Append(" ORDER BY LedgerInfoManagement_Id DESC LIMIT @PageSize OFFSET @offset");

            var pageData = await db.DapperConnection.QueryAsync<LedgerManagement>(sb.ToString(), paramet);
            int totalPage = (int)Math.Ceiling(total / (double)request.PageSize);
            return new ApiPeaging<ApiResult<List<LedgerManagement>>>
            {
                Total = total,
                TotalPage = totalPage,
                Data = new ApiResult<List<LedgerManagement>>
                {
                    Code = ApiEnum.success,
                    Message = "查询成功",
                    Data = pageData.ToList()
                }
            };
        }
    }
}
